package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.notify.NotifyCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.notify.NotifyCreateForm;
import com.bwda.dsrs.systemservice.domain.form.notify.NotifyQueryForm;
import com.bwda.dsrs.systemservice.domain.form.notify.NotifyUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.notify.NotifyPo;
import com.bwda.dsrs.systemservice.domain.vo.notify.NotifyVo;
import com.bwda.dsrs.systemservice.service.NotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
/**
 * 系统消息控制类
 *
 * @author yanyuemei
 * @date 2019/4/27
 */
@RestController
@RequestMapping("/s/notify")
@Api(value = "/s/notify", tags = "")
public class NotifyController extends BaseController {

	@Autowired
	private NotifyService notifyService;

	@ApiOperation(value = "查询系统消息",notes = "查询",httpMethod = "GET")
	@Loggable(descp = "查询系统消息",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<NotifyVo> query(@ApiParam(value = "", required = true)@RequestParam Integer id) throws SystemServiceException {
		NotifyPo po = notifyService.queryWithValid(id);
		NotifyVo vo = CopyUtil.transfer(po, NotifyVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询系统消息数量",notes = "查询数量",httpMethod = "GET")
	@Loggable(descp = "查询系统消息数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid NotifyQueryForm form) throws SystemServiceException {
		NotifyCondition condition = this.getConditionByQueryForm(form);
		int count = notifyService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询系统消息列表",notes = "查询列表",httpMethod = "GET")
	@Loggable(descp = "查询系统消息列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<NotifyVo>> queryList(@ModelAttribute@Valid NotifyQueryForm form) throws SystemServiceException {
		NotifyCondition condition = this.getConditionByQueryForm(form);
		List<NotifyPo> poList = notifyService.queryList(condition);
		List<NotifyVo> voList = CopyUtil.transfer(poList, NotifyVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询系统消息最新列表",notes = "查询最新列表",httpMethod = "GET")
	@Loggable(descp = "查询系统消息最新列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryLastNotify", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<NotifyPo> queryLastNotify() throws SystemServiceException {
		NotifyPo notifyPo = notifyService.queryLastNotify();
		return getSuccessResult(notifyPo);
	}

	@ApiOperation(value = "查询系统消息列表(带分页)",notes = "查询列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询系统消息列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<NotifyVo>> queryPageList(@ModelAttribute@Valid NotifyQueryForm form) throws SystemServiceException {
		NotifyCondition condition = this.getConditionByQueryForm(form);
		List<NotifyVo> voList = new ArrayList<>();
		int count = notifyService.queryCount(condition);
		if (count > 0) {
			List<NotifyPo> poList = notifyService.queryList(condition);
			voList = CopyUtil.transfer(poList, NotifyVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "新增系统消息",notes = "新增",httpMethod = "POST")
	@Loggable(descp = "新增系统消息",type = LogTypeEnum.CREATE,include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<NotifyVo> add(@ModelAttribute@Valid NotifyCreateForm form) throws SystemServiceException {
		NotifyPo po = CopyUtil.transfer(form, NotifyPo.class);
		po.setId(1);
		notifyService.insert(po);
		NotifyVo vo = CopyUtil.transfer(po, NotifyVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "修改系统消息",notes = "修改",httpMethod = "POST")
	@Loggable(descp = "修改系统消息",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid NotifyUpdateForm form) throws SystemServiceException {
		NotifyPo po = CopyUtil.transfer(form, NotifyPo.class);
		notifyService.update(po);
		return getSuccessResult();
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "删除系统消息",notes = "删除",httpMethod = "POST")
	@Loggable(descp = "删除系统消息",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "", required = true)@RequestParam Integer id) throws SystemServiceException {
		notifyService.delete(id);
		return getSuccessResult();
	}

	/**
	 * NotifyQueryForm转换为NotifyCondition
	 *
	 * @param form
	 * @return
	 */
	private NotifyCondition getConditionByQueryForm(NotifyQueryForm form) {
		NotifyCondition condition = CopyUtil.transfer(form, NotifyCondition.class);
		return condition;
	}

}